import unittest

from django.test import TestCase, tag

from index.models import Book, PubName


# Create your tests here.


# class ExampleTest(TestCase):
#     def test_addition(self):
#         def addition(x, y):
#             return x + y
#
#         self.assertEqual(addition(1, 1), 2, 'ass is failed')  # 断言函数加和运算
#
#     def test_model(self):
#         pub1 = PubName.objects.create(pubname="程序帮出版社")  # 创建pubname实例，
#         book = Book.objects.create(title='Servlet', price='35.00', retail_price='35.00', pub=pub1)
#         self.assertTrue(book is not None)
#         self.assertNotEqual(Book.objects.count(), 8)  # 使用断言判断
#         self.assertEqual(Book.objects.count(), 9)
#
#     def test_view(self):
#         pub1 = PubName.objects.create(pubname="机械工业出版社")
#         book = Book.objects.create(title='Jsp', price='25.00', retail_price='25.00', pub=pub1)
#         response = self.client.get('/index/update_book/%d/' % book.id)
#         response['X-Token'] = 'C语言中文网'  # 自定义响应头
#         self.assertEqual(response.status_code, 200)
#         self.assertEqual(response["X-Token"], 'C语言中文网', 'it is not same')

class ExampleTest(TestCase):
    def setUp(self):
        print('我负责测试环境初始化')
        self.pub1 = PubName.objects.create(pubname="编程帮出版")

    @tag('tagone')  # 添加标记
    def test_model(self):
        print('执行test_model测试')
        book = Book.objects.create(title='Servlet', price='35.00', retail_price='35.00', pub=self.pub1)
        self.assertTrue(book is not None)
        self.assertNotEqual(Book.objects.count(), 8)
        self.assertEqual(Book.objects.count(), 1)

    @tag('tagsecond')  # 添加标记
    def test_view(self):
        print('执行test_view测试')
        Book.objects.create(title='Jsp', price='25.00', retail_price='25.00', pub=self.pub1)
        response = self.client.get('/index/all_book/')  # 视图访问获取response
        self.assertEqual(response.status_code, 200)

    def test_assert1(self):
        """
        django内置断言的方法测试
        """
        # 用来断言可执行对象的调用引发了异常，且在异常中发现了对应的信息
        with self.assertRaisesMessage(ValueError, 'invalid literal for int()'):
            int('a')

    def test_assert2(self):
        # 判断HTML是否相等基于HTML语义，常用来检验返回模板的视图
        self.assertHTMLEqual("<p>hello c.biancheng.net</p>", " <p>hello c.biancheng.net</p>")
        self.assertHTMLNotEqual("<p>hello c.biancheng.net</p>", "<p>hello c.biancheng.net</p>")

    def test_assert3(self):
        # 用来断言JSON字符串是否相等，校验JsonResponse视图返回对象
        self.assertJSONEqual('{"name":"John","age":15}', '{"age":15,"name":"John"}')
        self.assertJSONNotEqual('{"a":1,"b":2}', '{"a":2,"c":1}')

    def test_asseret4(self):
        # 断言查询集合是否与给定的列表内容相等，给定列表也可包含多个元素
        book = Book.objects.create(title='Servlet', price='35.00', retail_price='35.00', pub=self.pub1)
        # repr(a)将对象a转换为字符串格式
        self.assertQuerysetEqual(Book.objects.all(), [repr(book)])

    def tearDown(self):
        print('我负责清理测试环境')


class SkipTest(unittest.TestCase):
    @unittest.skip('不用测试A用例')
    def test_A(self):
        print('测试A')

    @unittest.skipIf(True, '跳过B')
    def test_B(self):
        print('测试B')

    @unittest.skipUnless(False, '跳过C')
    def test_C(self):
        print('测试C')
